package com.neuedu.api.collection;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/2414:48
 */
public class MyArrayList {
    public static void main(String[] args) {

        //面相接口编程  多态
        List list = new ArrayList();

        //添加元素
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add(new String("张三"));
        list.add(18);    //自动装箱   list.add(new Integer(18));

        list.add(false);


        List list2 = new ArrayList(list);

        System.out.println("list.size() = " + list.size());
        System.out.println("list2.size() = " + list2.size());


        // 获取长度
        int size = list.size();

        //获取元素
        // Object obj0 = list.get(0);
        // Object obj1 = list.get(1);
        // Object obj2 = list.get(2);
        // Object obj3 = list.get(3);
        // int obj4 =  (Integer)list.get(4);    //         int obj4 =  ((Integer)list.get(4)).intValue();
        // Object obj5 = list.get(5);
        //
        // System.out.println("obj0 = " + obj0);
        // System.out.println("obj1 = " + obj1);
        // System.out.println("obj2 = " + obj2);
        // System.out.println("obj3 = " + obj3);
        // System.out.println("obj4 = " + obj4+"  进行计算  obj4 + 10："+(obj4 + 10));
        // System.out.println("obj5 = " + obj5);


        //利用size 和 get方法遍历集合
        for (int i = 0; i < list.size(); i++) {
            System.out.println("遍历: list.get("+i+") = " + list.get(i));
        }


        //addAll

        List list3 = new ArrayList();
        list3.add("张三1");
        list3.add("张三2");
        list3.add("张三3");
        list3.add("张三4");

        ArrayList list4 = new ArrayList();

        list4.add("张三5");
        list4.addAll(list3);  //展开的形式添加
        System.out.println("list4.size() = " + list4.size());
        System.out.println("list4 = " + list4);


        List list5 = new ArrayList();
        list5.add(100);
        list5.add(200);
        list5.add(300);


        //元素插入
        list5.add(2,600);

        //设置断点看
        System.out.println(list5);




        //元素的移除
        ArrayList list6 = new ArrayList();

        Integer intNum1 = new Integer(100);
        Integer intNum2 = new Integer(200);
        Integer intNum3 = new Integer(300);
        Integer intNum4 = new Integer(200);

        list6.add(intNum1);
        list6.add(intNum2);
        list6.add(intNum3);
        list6.add(intNum4);

        System.out.println("list6 remove 之前 = " + list6);
        // list6.remove(intNum2);   //remove(Object)
        list6.remove(new Integer(200));   //remove(Object)
        System.out.println("list6 remove 之后 = " + list6);


        List list7 = new ArrayList();
        list7.add(100);
        list7.add(200);
        list7.add(300);

        List list8 = new ArrayList();
        list8.add(100);
        list8.add(200);

        System.out.println("removeAll1 list7 = " + list7);
        System.out.println("removeAll2 list8 = " + list8);
        list7.removeAll(list8);
        System.out.println("removeAll2 list7 = " + list7);
        System.out.println("removeAll2 list8 = " + list8);
        System.out.println();
        System.out.println();
        System.out.println();


        List list9 = new ArrayList();
        list9.add(100);
        list9.add(200);
        list9.add(300);

        List list10 = new ArrayList();
        list10.add(100);
        list10.add(400);

        System.out.println("retainAll1 list9 = " + list9);
        System.out.println("retainAll1 list10 = " + list10);
        list9.retainAll(list10);
        System.out.println("retainAll2 list9 = " + list9);
        System.out.println("retainAll2 list10 = " + list10);


        //清空
        list10.clear();
        System.out.println("list10.clear() = " + list10);



        List list11 = new ArrayList();
        list11.add("张三");
        list11.add("李四");
        list11.add("王五");
        list11.add("赵六");
        list11.add("周七");

        // list11.get(0)

        System.out.println("list11.subList(0,2) = " + list11.subList(0, 2));  // [0,2)


        System.out.println("\r\n\r\n\r\n\r\n查找某些元素");

        List list12 = new ArrayList();
        list12.add("张三");
        list12.add("李四");
        list12.add("王五");
        list12.add("赵六");
        list12.add("周七");
        list12.add("李四");

        System.out.println("list12 = " + list12);
        System.out.println("list12.contains(\"李四\") = " + list12.contains("李四"));
        System.out.println("list12.contains(\"测试\") = " + list12.contains("测试"));
        System.out.println("list12.contains(new String(\"李四\")) = " + list12.contains(new String("李四")));



        System.out.println();
        System.out.println("list12.indexOf(\"李四\") = " + list12.indexOf("李四"));
        System.out.println("list12.indexOf(\"测试\") = " + list12.indexOf("测试"));
        System.out.println("list12.indexOf(new String(\"李四\")) = " + list12.indexOf(new String("李四")));

        System.out.println();
        System.out.println("list12.lastIndexOf(\"李四\") = " + list12.lastIndexOf("李四"));
        System.out.println("list12.lastIndexOf(\"测试\") = " + list12.lastIndexOf("测试"));
        System.out.println("list12.lastIndexOf(new String(\"李四\")) = " + list12.lastIndexOf(new String("李四")));

        System.out.println("\r\n\r\ncontainsAll\r\n\r\n");

        List list13 = new ArrayList();
        list13.add("张三");
        list13.add("李四");
        list13.add("测试");

        System.out.println("list13 = " + list13);
        System.out.println("list12.containsAll(list13) = " + list12.containsAll(list13));

        System.out.println("list13.isEmpty() = " + list13.isEmpty());    //false

        list13.clear();
        System.out.println("list13.clear : list13.isEmpty() = " + list13.isEmpty()); //true


        System.out.println("\r\n\r\n\r\n\r\n\n元素的修改 ");

        List list14 = new ArrayList();
        list14.add("张三0");
        list14.add("李四1");
        list14.add("王五2");

        System.out.println(" list14.set(1,\"新的值\") list14 = " + list14);
        list14.set(1,"新的值");
        System.out.println(" list14.set(1,\"新的值\") list14 = " + list14);





    }
}
